package com.sxhuayuan.parking.service.impl;

import com.sxhuayuan.parking.compenent.Filter;
import com.sxhuayuan.parking.compenent.Page;
import com.sxhuayuan.parking.compenent.Pageable;
import com.sxhuayuan.parking.config.InvoiceStatus;
import com.sxhuayuan.parking.dao.BaseDao;
import com.sxhuayuan.parking.dao.CarPlateDao;
import com.sxhuayuan.parking.dao.InvoiceDao;
import com.sxhuayuan.parking.entity.Invoice;
import com.sxhuayuan.parking.entity.Member;
import com.sxhuayuan.parking.exception.SystemException;
import com.sxhuayuan.parking.service.InvoiceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * 发票记录-service实现类
 * 
 * @author helong
 * @create 2019/8/29 15:21
 */
@Service("invoiceServiceImpl")
public class InvoiceServiceImpl extends BaseServiceImpl<Invoice, Long> implements InvoiceService {
	@Autowired
	InvoiceDao dao;

	@Override
	public BaseDao<Invoice, Long> getDAO() {
		return dao;
	}

	@Override
	public Invoice findBySn(String sn) {
		return dao.findBySn(sn);
	}

	@Override
	public List<Invoice> findInvoiceWaitBycreateDate(Date startTime, Date endingTime) {
		return dao.findByStatusAndCreateDate(InvoiceStatus.invoiceWait, startTime, endingTime);
	}

	@Override
	public Page<Invoice> findPage(Pageable pageable, Member member) {
		StringBuilder sb = new StringBuilder("select i from Invoice i where i.member.id=" + member.getId());
		if (pageable.getFilters() != null) {
			for (Filter filter : pageable.getFilters()) {
				setCondition(sb, filter);
			}
		}
		sb.append(" order by i.createDate desc");
		return findPage(sb.toString(), sb.toString().replace("select i", "select count(i.id)"), pageable.getPageSize(), pageable.getPageNumber());
	}

	/** 组装查询条件 */
	private void setCondition(StringBuilder sb, Filter filter) {
		if (filter.getProperty() == null || "".equals(filter.getProperty()) || filter.getValue() == null || "".equals(filter.getValue())) {
			return;
		}
		switch (filter.getProperty()) {

		case "istartTime": {
			sb.append(" and i.createDate>='").append(filter.getValue()).append("' ");
			break;
		}

		case "iendTime": {
			sb.append(" and i.createDate<='").append(filter.getValue()).append("' ");
			break;
		}

		default:
		}

	}

}
